﻿package com.tvein.sanguo.swc.room
{
	//flash
	import flash.display.MovieClip;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.text.TextFormat;
	//A3D
	//custom
	import com.tvein.sanguo.swc.common.SwcEvent;
	import com.tvein.sanguo.swc.common.MP3Click;
//
	import com.tvein.sanguo.swc.room.RoomButton;
	
	public class RoomShell extends MovieClip
	{
		public static const NAME:String = "RoomShell";
		
		private var effectVol:int = 100;
		
		public function RoomShell()
		{
			stage?init(null):addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE , init);
			addEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);

			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.BOTTOM_LEFT;
			stage.addEventListener(Event.RESIZE, _resizeUI);
			
			var style:TextFormat = new TextFormat();
			style.size = 15;
			style.font = "LiSu";
			
			mcChat.txtChat.setStyle("textFormat", style);
			
			mcChat.txtChat.addEventListener(TextEvent.LINK,txtHandler);
			
			_resizeUI();
		}
		
		public function console(Type:String, params:Object):void
		{
			switch (Type)
			{
				case "setEffect":
					effectVol = params.Volume;
					break;
				case "addButton":
					addButton(params);
					break;
				case "setButtonVisible":
					setButtonVisible(params);
					break;
			}
		}

		public function chatVisible(isVisible:Boolean):void
		{
			mcChat.visible = isVisible;
		}
		
		public function roomMsg(roleName:String, interMsg:String, msgType:int = 0):void
		{
			var formattedMsg:String;
			
			switch(msgType)
			{
				case 0: //System
					formattedMsg = "<font color='#FFCC33'>" + interMsg + "</font>";
					break;
				case 1: //All
					formattedMsg = "<font color='#FF0000'>[所有]<a href='event:" + roleName + "'><u>" + roleName + "</u></a>:" + interMsg + "</font>";
					break;
				case 2: //Canton
					formattedMsg = "<font color='#00FF00'>[州域]<a href='event:" + roleName + "'><u>" + roleName + "</u></a>:" + interMsg + "</font>";
					break;
				case 3: //County
					formattedMsg = "<font color='#0000FF'>[郡域]<a href='event:" + roleName + "'><u>" + roleName + "</u></a>:" + interMsg + "</font>";
					break;
				case 4: //Fort
					formattedMsg = "<font color='#FFFF00'>[据点]<a href='event:" + roleName + "'><u>" + roleName + "</u></a>:" + interMsg + "</font>";
					break;
				case 5: //Place
					formattedMsg = "<font color='#467E1C'>[所在]<a href='event:" + roleName + "'><u>" + roleName + "</u></a>:" + interMsg + "</font>";
					break;
				case 6: //Camp
					formattedMsg = "<font color='#216371'>[势力]<a href='event:" + roleName + "'><u>" + roleName + "</u></a>:" + interMsg + "</font>";
					break;
				case 7: //Guild
					formattedMsg = "<font color='#7D661C'>[行会]<a href='event:" + roleName + "'><u>" + roleName + "</u></a>:" + interMsg + "</font>";
					break;
				case 8: //Private
					formattedMsg = "<font color='#803C1C'>[密语]<a href='event:" + roleName + "'><u>" + roleName + "</u></a>:" + interMsg + "</font>";
					break;
			}
			
			mcChat.txtChat.htmlText += formattedMsg + "<br/>";
			
			setChatVPosition();
		}
		
		function txtHandler(evt:TextEvent):void
		{
			var mp3Click:MP3Click = new MP3Click();
			mp3Click.playback(effectVol * .01);
			
			dispatchEvent(new SwcEvent(NAME, ["privateChat", evt.text]));
		}
		
		private function setChatVPosition():void
		{
			mcChat.txtChat.verticalScrollPosition = mcChat.txtChat.maxVerticalScrollPosition;
		}
		
		public function addButton(params:Object):void
		{			
			if (this.getChildByName(params.Name) == null)
			{
				var button:RoomButton = new RoomButton(params.Name, params.Type, params.CMD, params.isVisible);
			
				button.name = params.Name;
			
				button.y = stage.stageHeight - 51;
				button.x = stage.stageWidth - 110;
			
				button.addEventListener(MouseEvent.MOUSE_MOVE, onMove);
				button.addEventListener(MouseEvent.CLICK, onClick);
				
				this.addChild(button);
			}
			
			_resizeUI();
		}

		public function setButtonVisible(params:Object):void
		{
			if (this.getChildByName(params.buttonName))
			{
				var button:RoomButton = RoomButton(this.getChildByName(params.buttonName));
				button.Visible(params.isVisible);
			}
		}
		
		private function onMove(evt:MouseEvent):void
		{
			evt.target.parent.parent.setChildIndex(evt.target.parent, evt.target.parent.parent.numChildren - 1);
		}
		
		private function onClick(evt:MouseEvent):void
		{
			if (evt.target.parent.hasOwnProperty("buttonCMD"))
			{
				var mp3Click:MP3Click = new MP3Click();
				mp3Click.playback(effectVol * .01);
			
				dispatchEvent(new SwcEvent(NAME, ["buttonCMD", evt.target.parent.buttonCMD]));
			}
			
			clearButton();
		}
		
		public function clearButton():void
		{
			while (this.numChildren > 3)
			{
				this.removeChildAt(3);
			}
			
			_resizeUI();
		}
		
		private function _onRemoved(evt:Event):void
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.removeEventListener(Event.RESIZE, _resizeUI);
			
			//remove button eventlistener
		}
		
		private function _resizeUI(evt:Event = null):void
		{
			var shellNum:int = 4;
			
			var frameWidth:int = 300;
			var frameHeight:int = stage.stageHeight * .6;
			
			if (frameHeight > 40) //mini size
			{
				mcChat.txtChat.width = frameWidth - mcChat.mcFrame4.width - mcChat.mcFrame6.width;
				mcChat.txtChat.height = frameHeight - mcChat.mcFrame2.height - mcChat.mcFrame8.height;
			
				mcChat.mcFrame2.width = mcChat.mcFrame8.width = frameWidth - mcChat.mcFrame1.width - mcChat.mcFrame3.width;
				mcChat.mcFrame4.height = mcChat.mcFrame6.height = frameHeight - mcChat.mcFrame1.height - mcChat.mcFrame7.height;
			
				mcChat.mcFrame1.x = mcChat.mcFrame4.x = mcChat.mcFrame7.x = 0;
				mcChat.mcFrame2.x = mcChat.mcFrame8.x = mcChat.mcFrame1.width;
				mcChat.mcFrame3.x = mcChat.mcFrame9.x = mcChat.mcFrame1.width + mcChat.mcFrame2.width;
				mcChat.mcFrame6.x = frameWidth - mcChat.mcFrame6.width;
			
				mcChat.mcFrame1.y = mcChat.mcFrame2.y = mcChat.mcFrame3.y = 0;
				mcChat.mcFrame4.y = mcChat.mcFrame6.y = mcChat.mcFrame1.height;
				mcChat.mcFrame7.y = mcChat.mcFrame9.y = mcChat.mcFrame1.height + mcChat.mcFrame4.height;
				mcChat.mcFrame8.y = frameHeight - mcChat.mcFrame8.height;
			
				mcChat.x = 10;
				mcChat.y = stage.stageHeight * .4 - 60;
			}
			else
			{
				mcChat.y = stage.stageHeight - 100;
			}

			shellLeft.y = shellCenter.y = stage.stageHeight - shellLeft.height;
			
			shellCenter.width = stage.stageWidth;

			var numRoom:int = this.numChildren - shellNum;
			
			var tmpX:int = (stage.stageWidth - mcChat.width - 110) / numRoom;
			
			if (tmpX > 110)
			{
				tmpX = 110;
			}
			
			var roomX:int = stage.stageWidth - 110;
			
			for (var i:int = (shellNum - 1); i < this.numChildren; i++)
			{				
				if (this.getChildAt(i).name != "shellLeft")
				{
					this.getChildAt(i).y = stage.stageHeight - 51;
					
					if (roomX > 320)
					{
						this.getChildAt(i).x = roomX;
					}
					else
					{
						this.getChildAt(i).x = 320;
					}
					
					roomX -= tmpX;
				}
			}
		}
	}
}